//1. 引入
// const mongoose = require('mongoose');

// //2. 连接
// mongoose.connect('mongodb://127.0.0.1:27017/db');

// //3. 创建连接回调
// mongoose.connection.on('open', () => {
//     //4. 创建结构对象
//     const StudentSchema = new mongoose.Schema({
//         name: String,
//         age: Number,
//         gender: String,
//     });

//     //5. 创建模型对象
//     const StudentModel = mongoose.model('student', StudentSchema);

//     //6. 文档操作
//     StudentModel.find((err, data) => {
//         if(err) throw err;
//         console.log(data);
//         mongoose.connection.close();
//     })
// });

//Promise
const mongoose = require('mongoose');

let res = mongoose.connect('mongodb://127.0.0.1:27017/db')

res.then(() => {
    //连接成功
    // 4. 创建结构对象
    const StudentSchema = new mongoose.Schema({
        name: String,
        age: Number,
        gender: String,
    });

    //5. 创建模型对象
    const StudentModel = mongoose.model('student', StudentSchema);

    //读取数据
    return StudentModel.find();
}, () => {
    //连接失败
}).then(v => { 
    console.log(v);
})




